package com.hx.miniblog.controller.business;

import com.hx.miniblog.annotation.CheckLogin;
import com.hx.miniblog.annotation.PassToken;
import com.hx.miniblog.controller.BaseController;
import com.hx.miniblog.pojo.mj.RoleType;
import com.hx.miniblog.pojo.ro.LoginRo;
import com.hx.miniblog.pojo.vo.LoginVo;
import com.hx.miniblog.service.SysUserService;
import com.hx.miniblog.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author wenhao hu
 * @title: SysLoginController
 * @projectName miniBlog
 * @description: TODO
 * @date 2021/12/2914:59
 */
@RestController
@RequestMapping("/api/sys-login")
@Api(tags = "登录相关接口",value = "SysLoginController")
public class SysLoginController extends BaseController {
    @Autowired
    private SysUserService userService;

    @PostMapping("/login")
    @ApiOperation(value = "登录")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "form", name = "userName", value = "账号", dataType = "String", required = true),
            @ApiImplicitParam(paramType = "form", name = "password", value = "密码", dataType = "String", required = true)
    })
    @PassToken
    public Result<LoginVo> login(LoginRo ro){
        return Result.OK(userService.login(ro));
    }

    @PostMapping("/logout")
    @ApiOperation(value = "退出登录")
    @CheckLogin(roles = {RoleType.ADMIN,RoleType.USER})
    public Result<Void> logout(){
        userService.logOut(currentUser());
        return Result.OK();
    }
}
